﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using MyBetImporter.DAL;

namespace MyBetImporter
{
    public class ContestObject
    {

        private int _championshipId;
        private int _seasonId;
       
        [Key()]
        [DataMemberAttribute()]
        public int ChampionshipId
        {
            get { return _championshipId; }
            set { _championshipId = value; }
        }

        [DataMemberAttribute()]
        public int SeasonId
        {
            get { return _seasonId; }
            set { _seasonId = value; }
        }

        /// <summary>
        /// Create object from database information
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static ContestObject CopyFrom(Contests contest)
        {
            ContestObject contestTgt = new ContestObject();
            contestTgt.ChampionshipId = contest.ChampionshipId;
            contestTgt.SeasonId = contest.SeasonId;
            return contestTgt;
        }

        /// <summary>
        /// Return Id from championship id and season id
        /// </summary>
        /// <param name="championshipId"></param>
        /// <param name="seasonId"></param>
        /// <returns></returns>
        public static int? GetId(ParieurProDBEntities db, int championshipId, int seasonId)
        {
            var query = (from contest in db.Contests
                         where contest.ChampionshipId == championshipId && contest.SeasonId == seasonId
                         select contest.ContestId);
            IList<int> ids = query.ToList();
            if (ids.Count != 1)
            {
                // contest not defined
                return null;
            }
            return ids[0];          
        }

    }
}